﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ComUIControl
{
    public partial class Billboard : UserControl, INotifyPropertyChanged
    {
        /// <summary>
        /// 提示内容
        /// </summary>
        private string _title;
        public string Title
        {
            get
            {
                return _title;
            }
            set
            {
                if (_title != value)
                {
                    _title = value;
                    if (!this.InvokeRequired)
                    {
                        labelTip.Text = value;
                    }
                    this.RaisePropertyChanged("Title");
                }
            }
        }

        /// <summary>
        /// 提示文字的颜色
        /// </summary>
        public Color TitleColor
        {
            get
            {
                return labelTip.ForeColor;
            }
            set
            {
                if (labelTip.ForeColor != value)
                {
                    labelTip.ForeColor = value;
                    this.RaisePropertyChanged("TitleColor");
                }
            }
        }

        /// <summary>
        /// 提示文字的字体
        /// </summary>
        public Font TitleFont
        {
            get
            {
                return labelTip.Font;
            }
            set
            {
                if (labelTip.Font != value)
                {
                    labelTip.Font = value;
                    this.RaisePropertyChanged("TitleFont");
                }
            }
        }

        public Billboard()
        {
            this.Visible = false;
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            InitializeComponent();
            labelTip.DataBindings.Add("Text", this, "Title");
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public delegate void UpdatePropertyDelegate(string strName);
        private void RaisePropertyChanged(string propertyName)
        {
            if (this.InvokeRequired)
            {
                UpdatePropertyDelegate outdelegate = new UpdatePropertyDelegate(RaisePropertyChanged);
                this.BeginInvoke(outdelegate, new object[] { propertyName });
                return;
            }
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public delegate void ShowTitleDelegate(IWin32Window owner, string title);
        public void Show(IWin32Window owner, string title)
        {         
            this.Visible = true;
            Title = title;
            ResetLocation(owner);
            this.Refresh();
        }

        public delegate void ShowTitleExDelegate(Control owner, string title, int height);
        public void Show(Control owner, string title, int height)
        {           
            this.Visible = true;
            Title = title;
            ReLayout(owner, height);
            this.Refresh();
        }

        public delegate void HideDelegate();
        public void HideWnd()
        {           
            this.Visible = false;
        }

        private void ResetLocation(IWin32Window owner)
        {
            Form _owner = (owner as Form == null) ? ((UserControl)owner).ParentForm : (Form)owner;           
            Size titleSize = labelTip.Size;
            int height = titleSize.Height + labelTip.Location.Y * 2; 
            int width = titleSize.Width < _owner.Width ? titleSize.Width : _owner.Width;
            this.Size = new Size(width, height);
          
            int x = /*_owner.Location.X +*/ (_owner.Width - this.Width) / 2;
            int y = /*_owner.Location.Y +*/ 20;
            if (x < 0)
                x = 0;
            if (y < 0)
                y = 0;

            this.Location = new Point(x, y);
            this.BackColor = Color.FromArgb(15, 0, 100, 200);
        }

        private void ReLayout(Control parent, int y)
        {
            if(parent == null)
            {
                return;
            }
            
            this.Parent = parent;
            
            Size titleSize = labelTip.Size;           
            int height = titleSize.Height + labelTip.Location.Y * 2;
            int width = titleSize.Width < parent.Width ? titleSize.Width : parent.Width;
            this.Size = new Size(width, height);
           
            int x = /*_owner.Location.X +*/ (parent.Width - this.Width) / 2;
            if (x < 0)
                x = 0;
            if (y < 0)
                y = 0;
            this.Location = new Point(x, y);
            this.BackColor = Color.FromArgb(15, 0, 100, 200);
        }


        public void SetFontColor(Color value)
        {          
            labelTip.ForeColor = value;
        }

    }
}
